# -*- coding: utf-8 -*-
'''
@Time    : 2021/7/30 9:45
@Author  : xhweng
@File    : testapi.py
'''
import json
from datetime import datetime

import requests


class WechatTestApi():

    def __init__(self):
        self.version = "1.0"
        self.corpid = ""
        self.corpsecret = ""

        self.test_get_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        self.send_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send"
        # 用于微信通知添加后测试使用
        # self.wechat_info = {
        #     "msgtype": "text",
        # }
        self.msgtype = "text"

    def get_token(self, corpId, corpsecret):
        params = {
            'corpId': corpId,
            'corpsecret': corpsecret
        }
        try:
            token_file = requests.post(self.test_get_token_url, params=params, timeout=10)
            print(token_file.json())
            return token_file.json()
        except Exception as e:
            print(e)
            pass
        return {}

    # 用于网络开发消息通知
    def send_msg(self):
        corpid = "wwc1f5990a8d8bebb7"
        corpsecret = "MJIaRY2d0ojC76dsbbTEKght2R-2RxkJv-FJD28EI9U"
        send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send'
        token = self.get_token(corpid, corpsecret)['access_token']
        params = {'access_token': token}
        body = {
            "touser": "@all",
            "toparty": 2,
            "msgtype": "text",
            "agentid": '1000003',
            "text": {
                "content": f'DjangoCeleryTest测试【Crontab】定时任务{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}',
            },
            "safe": "0",
        }
        res = requests.post(send_url, params=params, data=json.dumps(body))
        print(res.json())
        return res


if __name__ == '__main__':
    pass
